//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#pragma package(smart_init)
#pragma resource "*.dfm"

//---------------------------------------------------------------------------
#include "vSSupprAdresse.h"
//---------------------------------------------------------------------------
//                      CONSTRUCTEUR
//---------------------------------------------------------------------------
__fastcall TSupprimerAdresse::TSupprimerAdresse(const AnsiString &asLogiciel,
                                                const AnsiString &asVersion,
                                                const int &nLogiciel,
                                                const int &nVersion,
                                                const AnsiString &asRefNom,
                                                TComponent* Owner)
    : TForm(Owner)
{
     try {
         dmWebHelp = new TdmWebHelp((TComponent*)0);

         m_asLogiciel = asLogiciel;
         m_asVersion  = asVersion;
         m_nLogiciel  = nLogiciel;
         m_nVersion   = nVersion;
         m_asRef_Nom  = asRefNom;

         lText->Caption = "Il n'existe plus d'adresse Web attachée à la référence : ";
         lRef->Caption = m_asRef_Nom;

         if(VerifAdresse() ) {
            rbConserver->Checked = true;
         }else {
            rbSupprimer->Checked = true;
            cbAttente->Enabled = false;
         }

         //Remplissage de la combo attente
         //-------------------------------

         CComboAttente * Attente = NULL;
         Attente = new CComboAttente();
         Attente->Remplir(cbAttente->Items);
         if(Attente) {delete Attente; Attente = NULL;}

         cbAttente->ItemIndex = 0;
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
__fastcall TSupprimerAdresse::~TSupprimerAdresse()
{
    try {
        if(dmWebHelp) {delete dmWebHelp; dmWebHelp = NULL;}
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//                  RENDRE INACTIVE LA COMBO
//---------------------------------------------------------------------------
void __fastcall TSupprimerAdresse::rbSupprimerClick(TObject *Sender)
{
    try {
        cbAttente->Enabled = false;
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//                  RENDRE ACTIVE LA COMBO
//---------------------------------------------------------------------------
void __fastcall TSupprimerAdresse::rbConserverClick(TObject *Sender)
{
    try {
        cbAttente->Enabled = true;
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//                  SUPPRIMER OU CONSERVER LA REFERENCE
//---------------------------------------------------------------------------
void __fastcall TSupprimerAdresse::bbOkClick(TObject *Sender)
{
    CHistorique * Hist = NULL;
    try {

        Hist = new CHistorique();
        bool bAction = false;
        Supprimer();
        if(rbConserver->Checked == true){
            Conserver();
            bAction = true;
        }

        if(bAction)
            Hist->AjouterOperation(Histo_SUPPR_ADRESSE,m_asLogiciel, m_asVersion,m_asRef_Nom);
        else
            Hist->AjouterOperation(Histo_SUPPR_REF,m_asLogiciel, m_asVersion,m_asRef_Nom);
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(Hist) {delete Hist; Hist = NULL;}
}
//---------------------------------------------------------------------------
//                       SUPPRIMER
//---------------------------------------------------------------------------
void __fastcall TSupprimerAdresse::Supprimer()
{
    TZZMySqlQuery * MaRequete = NULL;
    try {

        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        MaRequete->Sql->Text =  (AnsiString)"delete from refer "
                                            "where ref_nom = \""+m_asRef_Nom+"\""+
                                            " and log_code="+m_nLogiciel+
                                            " and ver_code="+m_nVersion;
        MaRequete->ExecSql();
        MaRequete->Sql->Clear();

        MaRequete->Sql->Text = "Commit";
        MaRequete->ExecSql();
        MaRequete->Sql->Clear();

        MaRequete->Close();

    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
}
//---------------------------------------------------------------------------
//                        CONSERVER
//---------------------------------------------------------------------------
void __fastcall TSupprimerAdresse::Conserver()
{
    try {
        dmWebHelp->tRefer->Active = true;
        dmWebHelp->tRefer->Insert();
        ntRefer(LOG_CODE)        = m_nLogiciel;
        ntRefer(VER_CODE)        = m_nVersion;
        astRefer(REF_NOM)        = m_asRef_Nom;
        astRefer(REF_SOUS_NOM)   = "";
        astRefer(REF_COMMENTAIRE)= "";
        ntRefer(REF_ATTENTE)     = (int)cbAttente->Items->Objects[cbAttente->ItemIndex];
        dmWebHelp->tRefer->Post();
        dmWebHelp->trWebHelp->Commit();
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//                        Verifier l'adresse web
//---------------------------------------------------------------------------
bool __fastcall TSupprimerAdresse::VerifAdresse()
{
    bool bRetour = false;
    TZZMySqlQuery * MaRequete = NULL;
    try {

        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        MaRequete->Sql->Text = (AnsiString) "select ref_adresse, ref_attente "+
                                            "from refer "+
                                            "where (log_code="+m_nLogiciel+") "+
                                                "and (ver_code="+m_nVersion+") "+
                                                "and (ref_nom=\""+m_asRef_Nom+"\")";
        MaRequete->Open();
        MaRequete->First();

        if( (!asRequete(0).IsEmpty()) && (nRequete(1) == -1) ) {
            bRetour = true;
        }
        MaRequete->Sql->Clear();
        MaRequete->Close();

    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
        bRetour = false;}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
    return bRetour;
}
//---------------------------------------------------------------------------
